package br.com.gr.jcash.utils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Util {

    public static Date parseStringToDate(String data) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date date = dateFormat.parse(data);
            return date;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static BigDecimal parseStringToBigDecimal(String numero) {
        Locale Local = new Locale("pt", "BR");

        DecimalFormat df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(Local));
        df.setParseBigDecimal(true);
        try {
            return (BigDecimal) df.parseObject(numero); // vai retornar um double

        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Integer parseStringToInteger(String numero) {
        try {
            return Integer.parseInt(numero);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Boolean parseStringToBoolean(String string) {
        if (string.equalsIgnoreCase("true")) {
            return Boolean.TRUE;
        }
        if (string.equalsIgnoreCase("false")) {
            return Boolean.FALSE;
        }
        return null;
    }

    public static boolean ehNuloOuVazio(String string) {
        return (null == string || string.trim().length() == 0) ? true : false;
    }

    public static String parseDateToString(Date data) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(data);
    }

    public static String parseBigDecimalToString(BigDecimal numero) {
        Locale Local = new Locale("pt", "BR");

        // Number pra string
        DecimalFormat df = new DecimalFormat("#,##0.00",
                new DecimalFormatSymbols(Local));
        return df.format(numero);
    }

    public static Date getDataFimMes(Date data) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    
    public static Date getDataInicioMes(Date data) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
}
